﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.PublicOfficialSelectionService;
using CommunitySystem.Views;
using CommunitySystem.ZhaoLuService;
using EHR.Common;

namespace CommunitySystem
{
    public partial class ZhaoLuControl : UserControl
    {
        private ObservableCollection<ZhaoLuZhiWeiModel> selectedempmodel =
            new ObservableCollection<ZhaoLuZhiWeiModel>();
        private ZhaoLuService.ZhaoLuServiceClient Client1 = new ZhaoLuServiceClient();
        public ObservableCollection<ZhaoLuZhiWeiModel> empmodel = new ObservableCollection<ZhaoLuZhiWeiModel>();
        private EmpbasicService.EmpBasicServiceClient Client = new EmpbasicService.EmpBasicServiceClient();
        private PublicOfficialSelectionService.PublicOfficialSelectionServiceClient Client2 = new PublicOfficialSelectionService.PublicOfficialSelectionServiceClient();
        public ZhaoLuControl()
        {
            InitializeComponent();
            selectedempmodel.Clear();
            _gridClickManagerXQ = new MouseClickManager(DGRole, 300);
            _gridClickManagerXQ.DoubleClick += new System.Windows.Input.MouseButtonEventHandler(_gridClickManagerXQ_DoubleClick);
            Client.GetDengLuJueSeCompleted += new EventHandler<EmpbasicService.GetDengLuJueSeCompletedEventArgs>(Client_GetDengLuJueSeCompleted);
            Client.GetDengLuJueSeAsync(Session.LoginUser.LoginName);
            if (TreeOfLife.ItemsSource != null) TreeOfLife.ItemsSource = null;
            Client.GetOrgForHRModelCompleted += new EventHandler<EmpbasicService.GetOrgForHRModelCompletedEventArgs>(Client_GetOrgForHRModelCompleted);
            Client.GetOrgForHRModelAsync(0);
            for (int i = DateTime.Today.Year; i > DateTime.Today.Year - 10; i--)
            {
                ComboBoxItem cbi = new ComboBoxItem();
                cbi.Content = i.ToString();
                cbi.Tag = i;
                nian.Items.Add(cbi);
            }
            nian.SelectedIndex = 0;
            Load();
        }

        private string islock = "0";
        private void CheckBox_Checked(object sender, RoutedEventArgs e)
        {
            islock = "1";
        }

        private void chkHiddenCols_Unchecked(object sender, RoutedEventArgs e)
        {
            islock = "0";
        }
        void Client_GetDengLuJueSeCompleted(object sender, EmpbasicService.GetDengLuJueSeCompletedEventArgs e)
        {
            juese = e.Result;
            if (e.Result != "人事干部")
            {
                TreeOfLife.Visibility = System.Windows.Visibility.Visible;
                colLeft.Width = new GridLength(265);
                colSplit.Width = new GridLength(10);
                CheckData.Visibility = System.Windows.Visibility.Visible;
                TuiData.Visibility = System.Windows.Visibility.Visible;
                FaBuData.Visibility = System.Windows.Visibility.Visible;
                tiaomu.Visibility = System.Windows.Visibility.Visible;
                CancelCheckData.Visibility = System.Windows.Visibility.Visible;
            }
            else
            {
                TreeOfLife.Visibility = System.Windows.Visibility.Collapsed;
                colLeft.Width = new GridLength(0);
                colSplit.Width = new GridLength(0);
                CheckData.Visibility = System.Windows.Visibility.Collapsed;
                TuiData.Visibility = System.Windows.Visibility.Collapsed;
                FaBuData.Visibility = System.Windows.Visibility.Collapsed;
                tiaomu.Visibility = System.Windows.Visibility.Collapsed;
                CancelCheckData.Visibility = System.Windows.Visibility.Collapsed;
            }
        }

        #region 左右拖动的条
        private GridLength _lastColLeftLength;
        private GridLength _lastColRightLength;
        private void GridSpliteTo_ToLeft(object sender, EventArgs e)
        {
            //已经到最左边，直接返回
            if (colLeft.Width.Value == 0.0)
            {
                return;
            }

            //
            if (colRight.Width.Value == 0)
            {
                colLeft.Width = _lastColLeftLength;
                colRight.Width = _lastColRightLength;
            }
            else
            {
                _lastColLeftLength = colLeft.Width;
                _lastColRightLength = colRight.Width;

                colLeft.Width = new GridLength(0);
                colRight.Width = new GridLength(150, GridUnitType.Star);
            }
        }

        private void GridSpliteTo_ToRight(object sender, EventArgs e)
        {
            if (colRight.Width.Value == 0)
            {
                return;
            }

            if (colLeft.Width.Value == 0)
            {
                colLeft.Width = _lastColLeftLength;
                colRight.Width = _lastColRightLength;
            }
            else
            {
                _lastColLeftLength = colLeft.Width;
                _lastColRightLength = colRight.Width;

                colRight.Width = new GridLength(0);
                colLeft.Width = new GridLength(300, GridUnitType.Star);
            }
        }
        #endregion
        #region 翻页
        private void dataPager1_PageIndexChanged(object sender, EventArgs e)
        {
            int index = dataPager1.PageIndex;
            string ss = "";
            foreach (string ii in orgids)
                ss += ii + ",";
            if (DGRole.ItemsSource != null)
            {
                foreach (ZhaoLuZhiWeiModel i in (ObservableCollection<ZhaoLuZhiWeiModel>)DGRole.ItemsSource)
                {
                    if (i.IsSelected == true && selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() == null)
                    {
                        selectedempmodel.Add(i);
                    }
                    if (selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() != null && i.IsSelected == false)
                    {
                        selectedempmodel.Remove(selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault());
                    }
                }
            }
            if (string.IsNullOrEmpty(ss)) ss = "0";
            else ss = ss.Substring(0, ss.Length - 1);
            Client1.GetZhaoLuZhiWeiCompleted += new EventHandler<GetZhaoLuZhiWeiCompletedEventArgs>(Client1_GetZhaoLuZhiWeiCompleted);
            Client1.GetZhaoLuZhiWeiAsync(ss, 0, index*20, index*20 + 20,
                                         Convert.ToInt32(((ComboBoxItem) status.SelectedItem).Tag),
                                         DateTime.Today.Year - nian.SelectedIndex, islock);
        }

        void Client1_GetZhaoLuZhiWeiCompleted(object sender, GetZhaoLuZhiWeiCompletedEventArgs e)
        {
            foreach (ZhaoLuZhiWeiModel i in e.Result)
            {
                if (empmodel.Where(p => p.ID == i.ID).FirstOrDefault() == null)
                {
                    empmodel.Add(i);
                }
            }
            foreach (ZhaoLuZhiWeiModel i in selectedempmodel)
            {
                if (e.Result.Where(p => p.ID == i.ID).FirstOrDefault() != null && i.IsSelected == true)
                {
                    e.Result.Where(p => p.ID == i.ID).FirstOrDefault().IsSelected = true;
                }
            }
            DGRole.ItemsSource = e.Result;
        }
        #endregion
        #region treeview
        void Client_GetOrgForHRModelCompleted(object sender, EmpbasicService.GetOrgForHRModelCompletedEventArgs e)
        {
            forumList.Clear();
            foreach (EmpbasicService.OrgForHRModel i in e.Result)
            {
                forumList.Add(i);
            }
            AddTreeNode("全部单位", null);
        }
        ObservableCollection<EmpbasicService.OrgForHRModel> forumList = new ObservableCollection<EmpbasicService.OrgForHRModel>();


        private void AddTreeNode(string type, TreeViewItem treeViewItem)
        {
            List<EmpbasicService.OrgForHRModel> result = (from forumInfo in forumList
                                                          where forumInfo.Type == type
                                                          select forumInfo).ToList<EmpbasicService.OrgForHRModel>();

            if (result.Count > 0)
            {
                foreach (EmpbasicService.OrgForHRModel foruminfo in result)
                {
                    TreeViewItem objTreeNode = new TreeViewItem();
                    CheckBox cb = new CheckBox();
                    cb.Content = foruminfo.Name;
                    cb.Tag = objTreeNode;
                    cb.DataContext = foruminfo;
                    //sp.Children.Add(cb);
                    cb.Click += new RoutedEventHandler(cb_Checked);
                    objTreeNode.Header = cb;
                    if ((objTreeNode.Header as CheckBox).Content.ToString() == "组织机构") objTreeNode.IsExpanded = true;
                    //添加根节点
                    if (treeViewItem == null)
                    {
                        TreeOfLife.Items.Add(objTreeNode);
                    }
                    else
                    {
                        treeViewItem.Items.Add(objTreeNode);
                    }
                    AddTreeNode(foruminfo.Name, objTreeNode);
                }
            }
        }
        void cb_Checked(object sender, RoutedEventArgs e)
        {
            CheckBox cb = sender as CheckBox;
            EmpbasicService.OrgForHRModel rm = cb.DataContext as EmpbasicService.OrgForHRModel;
            if (cb.IsChecked == true)
            {
                //if (orgids != null)
                //{
                //    OrgID = Convert.ToInt32(((EmpbasicService.OrgForHRModel)cb.DataContext).ID);
                //    LoadData(OrgID);
                //    //if (OrgID != 0 && OrgID != 79 && OrgID != 80 && OrgID != 81 && OrgID != 82 && OrgID != 83 && OrgID != 84)
                //    //{

                //    //}
                //}
                //else orgids.Add(((EmpbasicService.OrgForHRModel)cb.DataContext).ID.ToString());
                if (!orgids.Contains(((EmpbasicService.OrgForHRModel)cb.DataContext).ID.ToString()))
                    orgids.Add(((EmpbasicService.OrgForHRModel)cb.DataContext).ID.ToString());
                string ss = "";
                foreach (string i in orgids)
                    ss += i + ",";
                LoadData(ss.Substring(0, ss.Length - 1));
                //CheckParentItem(cb.Tag as TreeViewItem);
                CheckChildItem(cb.Tag as TreeViewItem);
            }
            else
            {
                if (orgids != null)
                {
                    if (orgids.Contains(((EmpbasicService.OrgForHRModel)cb.DataContext).ID.ToString()))
                        orgids.Remove(((EmpbasicService.OrgForHRModel)cb.DataContext).ID.ToString());
                }
                string ss = "";
                foreach (string i in orgids)
                    ss += i + ",";
                LoadData(ss.Substring(0, ss.Length - 1));
                UnCheckChildItem(cb.Tag as TreeViewItem);
            }
        }
        void CheckParentItem(TreeViewItem item)
        {
            TreeViewItem tvi = item.GetParentTreeViewItem();
            if (tvi != null)
            {
                CheckBox cb = tvi.Header as CheckBox;
                cb.IsChecked = true;
                CheckParentItem(tvi);
            }
        }
        public List<string> orgids = new List<string>();
        void CheckChildItem(TreeViewItem item)
        {
            foreach (TreeViewItem tvi in item.Items)
            {
                if (tvi != null)
                {
                    CheckBox cb = tvi.Header as CheckBox;
                    cb.IsChecked = true;
                    if (!orgids.Contains(((EmpbasicService.OrgForHRModel)cb.DataContext).ID.ToString()))
                        orgids.Add(((EmpbasicService.OrgForHRModel)cb.DataContext).ID.ToString());
                    CheckChildItem(tvi);
                }
            }
        }
        void UnCheckChildItem(TreeViewItem item)
        {
            foreach (TreeViewItem tvi in item.Items)
            {
                if (tvi != null)
                {
                    CheckBox cb = tvi.Header as CheckBox;
                    cb.IsChecked = false;
                    if (orgids.Contains(((EmpbasicService.OrgForHRModel)cb.DataContext).ID.ToString()))
                        orgids.Remove(((EmpbasicService.OrgForHRModel)cb.DataContext).ID.ToString());
                    UnCheckChildItem(tvi);
                }
            }
        }
        #endregion
        #region 右键

        private readonly MouseClickManager _gridClickManagerXQ;
        private void dgXQ_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            e.Row.MouseLeftButtonUp += _gridClickManagerXQ.HandleClick;
            //右击小区
            RightMenuXQ(e);
        }

        private void RightMenuXQ(DataGridRowEventArgs e)
        {
            DataGridRow dgr = e.Row;
            ZhaoLuZhiWeiModel XQ = (ZhaoLuZhiWeiModel)dgr.DataContext;

            ContextMenu cm = new ContextMenu();
            System.Windows.Controls.MenuItem miUP = new System.Windows.Controls.MenuItem();
            miUP.Header = "编辑";
            miUP.Click += new RoutedEventHandler(miUP_Click);

            System.Windows.Controls.MenuItem miDE = new System.Windows.Controls.MenuItem();
            miDE.Header = "删除";
            miDE.Click += new RoutedEventHandler(miDE_Click);

            System.Windows.Controls.MenuItem miAdd = new System.Windows.Controls.MenuItem();
            miAdd.Header = "提交";
            miAdd.Click += new RoutedEventHandler(miAdd_Click);


            cm.Items.Add(miUP);
            cm.Items.Add(miDE);
            cm.Items.Add(miAdd);

            ContextMenuService.SetContextMenu(dgr, cm);
        }
        void miUP_Click(object sender, RoutedEventArgs e)
        {
            ZhaoLuZhiWeiModel selxq = ((System.Windows.Controls.MenuItem)sender).DataContext as ZhaoLuZhiWeiModel;

            if (selxq != null)
            {
                if (selxq.Status != 0 && selxq.Status != 2)
                {
                    SilverlightMessageBoxLibrary.Message.ErrorMessage("所选职位信息状态为不可编辑状态！");
                    return;
                }
                ZhaoLuWindow RMWindow = new ZhaoLuWindow(new List<string>(0), false, selxq.ID, "",islock);
                RMWindow.Closed += new EventHandler(RMWindow_Closed);
                RMWindow.Show();

            }
            else SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择要编辑的职位信息！");
        }
        void miDE_Click(object sender, RoutedEventArgs e)
        {
            ZhaoLuZhiWeiModel selxq = ((System.Windows.Controls.MenuItem)sender).DataContext as ZhaoLuZhiWeiModel;

            if (selxq != null)
            {
                if (MessageBox.Show("确定删除吗？", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    Client1.DeteleZhaoLuZhiWeiCompleted -= new EventHandler<DeteleZhaoLuZhiWeiCompletedEventArgs>(Client1_DeteleZhaoLuZhiWeiCompleted);
                    Client1.DeteleZhaoLuZhiWeiCompleted += new EventHandler<DeteleZhaoLuZhiWeiCompletedEventArgs>(Client1_DeteleZhaoLuZhiWeiCompleted);
                    Client1.DeteleZhaoLuZhiWeiAsync("(" + selxq.ID.ToString() + ")");
                }

            }
            else SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择要删除的职位信息！");
        }

        void Client1_DeteleZhaoLuZhiWeiCompleted(object sender, DeteleZhaoLuZhiWeiCompletedEventArgs e)
        {
            SilverlightMessageBoxLibrary.Message.InfoMessage("删除成功！");
            Load();
        }
        void miAdd_Click(object sender, RoutedEventArgs e)
        {
            ZhaoLuZhiWeiModel selxq = ((System.Windows.Controls.MenuItem)sender).DataContext as ZhaoLuZhiWeiModel;

            if (selxq != null)
            {
                if (selxq.Status == 1 || selxq.Status == 3 || selxq.Status == 4)
                {
                    SilverlightMessageBoxLibrary.Message.ErrorMessage("所选职位信息状态为不可提交状态！");
                    return;
                }
                if (MessageBox.Show("确定提交吗？", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    Client1.SaveZhaoLuZhiWeiCompleted -= new EventHandler<SaveZhaoLuZhiWeiCompletedEventArgs>(Client1_SaveZhaoLuZhiWeiCompleted);
                    Client1.SaveZhaoLuZhiWeiCompleted += new EventHandler<SaveZhaoLuZhiWeiCompletedEventArgs>(Client1_SaveZhaoLuZhiWeiCompleted);
                    Client1.SaveZhaoLuZhiWeiAsync("(" + selxq.ID.ToString() + ")");
                }

            }
            else SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择要提交的职位信息！");
        }

        void Client1_SaveZhaoLuZhiWeiCompleted(object sender, SaveZhaoLuZhiWeiCompletedEventArgs e)
        {
            SilverlightMessageBoxLibrary.Message.InfoMessage("提交成功！");
            Load();
        }
        private void _gridClickManagerXQ_DoubleClick(object sender, MouseButtonEventArgs e)
        {
            var selxq = this.DGRole.SelectedItem as ZhaoLuZhiWeiModel;
            if (selxq != null)
            {
                if (selxq.Status != 0 && selxq.Status != 2)
                {
                    SilverlightMessageBoxLibrary.Message.ErrorMessage("所选职位信息状态为不可编辑状态！");
                    return;
                }
                ZhaoLuWindow RMWindow = new ZhaoLuWindow(new List<string>(0), false, selxq.ID, "", islock);
                RMWindow.Closed += new EventHandler(RMWindow_Closed);
                RMWindow.Show();

            }
            else SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择要编辑的职位信息！");
        }
        private void CheckBox_Click(object sender, RoutedEventArgs e)
        {
            CheckBox cb = sender as CheckBox;
            foreach (ZhaoLuZhiWeiModel p in DGRole.ItemsSource)
            {
                p.IsSelected = (bool)cb.IsChecked;
                //CheckBox cb1 = DGRole.Columns[0].GetCellContent(p) as CheckBox;
                //cb1.IsChecked = p.IsSelected;
                if (p.IsSelected)
                {
                    if (selectedempmodel.Where(i => i.ID == p.ID).FirstOrDefault() == null)
                    {
                        selectedempmodel.Add(p);
                    }
                }
                else
                {
                    if (selectedempmodel.Where(i => i.ID == p.ID).FirstOrDefault() != null)
                    {
                        selectedempmodel.Remove(selectedempmodel.Where(i => i.ID == p.ID).FirstOrDefault());
                    }
                }
            }

        }
        #endregion
        public void Load()
        {

            Client2.GetOrgIDbyDengLuRenCompleted -= new EventHandler<PublicOfficialSelectionService.GetOrgIDbyDengLuRenCompletedEventArgs>(Client_GetOrgIDbyDengLuRenCompleted);
            Client2.GetOrgIDbyDengLuRenCompleted += new EventHandler<PublicOfficialSelectionService.GetOrgIDbyDengLuRenCompletedEventArgs>(Client_GetOrgIDbyDengLuRenCompleted);
            Client2.GetOrgIDbyDengLuRenAsync(Session.LoginUser.UserID);
        }
        void Client_GetOrgIDbyDengLuRenCompleted(object sender, PublicOfficialSelectionService.GetOrgIDbyDengLuRenCompletedEventArgs e)
        {
            string[] ss = e.Result.Split(',');
            foreach (string i in ss)
            {
                Client2.GetTypebyOrgIDCompleted -= new EventHandler<PublicOfficialSelectionService.GetTypebyOrgIDCompletedEventArgs>(Client_GetTypebyOrgIDCompleted);
                Client2.GetTypebyOrgIDCompleted += new EventHandler<PublicOfficialSelectionService.GetTypebyOrgIDCompletedEventArgs>(Client_GetTypebyOrgIDCompleted);
                Client2.GetTypebyOrgIDAsync(Convert.ToInt32(i));
            }

        }
        void Client_GetTypebyOrgIDCompleted(object sender, PublicOfficialSelectionService.GetTypebyOrgIDCompletedEventArgs e)
        {
            if (e.Result != null)
            {
                foreach (string i in e.Result.Keys)
                {
                    
                        //OrgID = Convert.ToInt32(e.Result[i]);
                        if (!orgids.Contains(e.Result[i].ToString()))
                            orgids.Add(e.Result[i].ToString());
                        string ss = "";
                        foreach (string ii in orgids)
                            ss += ii + ",";
                        if (string.IsNullOrEmpty(ss)) ss = "0";
                        else ss = ss.Substring(0, ss.Length - 1);
                        Client1.ChaKanShiFouDouShenHeZhaoLuZhiWeiCompleted -= new EventHandler<ChaKanShiFouDouShenHeZhaoLuZhiWeiCompletedEventArgs>(Client1_ChaKanShiFouDouShenHeZhaoLuZhiWeiCompleted);
                        Client1.ChaKanShiFouDouShenHeZhaoLuZhiWeiCompleted += new EventHandler<ChaKanShiFouDouShenHeZhaoLuZhiWeiCompletedEventArgs>(Client1_ChaKanShiFouDouShenHeZhaoLuZhiWeiCompleted);
                        Client1.ChaKanShiFouDouShenHeZhaoLuZhiWeiAsync(ss, DateTime.Today.Year - nian.SelectedIndex, islock);
                        Client1.GetZhaoLuZhiWeiCountCompleted -= new EventHandler<GetZhaoLuZhiWeiCountCompletedEventArgs>(Client1_GetZhaoLuZhiWeiCountCompleted);
                        Client1.GetZhaoLuZhiWeiCountCompleted += new EventHandler<GetZhaoLuZhiWeiCountCompletedEventArgs>(Client1_GetZhaoLuZhiWeiCountCompleted);
                        Client1.GetZhaoLuZhiWeiCountAsync(ss, 0,
                                                          Convert.ToInt32(((ComboBoxItem) status.SelectedItem).Tag),
                                                          DateTime.Today.Year - nian.SelectedIndex, islock);
                        Client1.GetZhaoLuZhiWeiCompleted -= new EventHandler<GetZhaoLuZhiWeiCompletedEventArgs>(Client1_GetZhaoLuZhiWeiCompleted);
                        Client1.GetZhaoLuZhiWeiCompleted+=new EventHandler<GetZhaoLuZhiWeiCompletedEventArgs>(Client1_GetZhaoLuZhiWeiCompleted);
                        Client1.GetZhaoLuZhiWeiAsync(ss, 0, 0, 20,
                                                     Convert.ToInt32(((ComboBoxItem) status.SelectedItem).Tag),
                                                     DateTime.Today.Year - nian.SelectedIndex, islock);
                    

                }
            }

        }

        void Client1_GetZhaoLuZhiWeiCountCompleted(object sender, GetZhaoLuZhiWeiCountCompletedEventArgs e)
        {
            int count = e.Result;
            ObservableCollection<int> source = new ObservableCollection<int>();
            for (int i = 0; i < count; i++)
            {
                source.Add(i + 1);

            }
            PagedCollectionView pcv = new PagedCollectionView(source);
            dataPager1.PageSize = 1;
            dataPager1.Source = pcv;
        }

        void Client1_ChaKanShiFouDouShenHeZhaoLuZhiWeiCompleted(object sender, ChaKanShiFouDouShenHeZhaoLuZhiWeiCompletedEventArgs e)
        {
            if (e.Result != null)
                tiaomu.Text = "剩余" + e.Result + "条职位信息没有审核";
            else tiaomu.Text = "没有待审核的职位信息";
        }
        public void LoadData(string orgid)
        {
            Client1.ChaKanShiFouDouShenHeZhaoLuZhiWeiCompleted -= new EventHandler<ChaKanShiFouDouShenHeZhaoLuZhiWeiCompletedEventArgs>(Client1_ChaKanShiFouDouShenHeZhaoLuZhiWeiCompleted);
            Client1.ChaKanShiFouDouShenHeZhaoLuZhiWeiCompleted += new EventHandler<ChaKanShiFouDouShenHeZhaoLuZhiWeiCompletedEventArgs>(Client1_ChaKanShiFouDouShenHeZhaoLuZhiWeiCompleted);
            Client1.ChaKanShiFouDouShenHeZhaoLuZhiWeiAsync(orgid, DateTime.Today.Year - nian.SelectedIndex, islock);
            Client1.GetZhaoLuZhiWeiCountCompleted -= new EventHandler<GetZhaoLuZhiWeiCountCompletedEventArgs>(Client1_GetZhaoLuZhiWeiCountCompleted);
            Client1.GetZhaoLuZhiWeiCountCompleted += new EventHandler<GetZhaoLuZhiWeiCountCompletedEventArgs>(Client1_GetZhaoLuZhiWeiCountCompleted);
            Client1.GetZhaoLuZhiWeiCountAsync(orgid, 0, Convert.ToInt32(((ComboBoxItem)status.SelectedItem).Tag), DateTime.Today.Year - nian.SelectedIndex, islock);
            Client1.GetZhaoLuZhiWeiCompleted -= new EventHandler<GetZhaoLuZhiWeiCompletedEventArgs>(Client1_GetZhaoLuZhiWeiCompleted);
            Client1.GetZhaoLuZhiWeiCompleted += new EventHandler<GetZhaoLuZhiWeiCompletedEventArgs>(Client1_GetZhaoLuZhiWeiCompleted);
            Client1.GetZhaoLuZhiWeiAsync(orgid, 0, 0, 20, Convert.ToInt32(((ComboBoxItem)status.SelectedItem).Tag), DateTime.Today.Year - nian.SelectedIndex, islock);

        }

        #region 操作
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void NewData_Click(object sender, RoutedEventArgs e)
        {
            //if (orgids.Count > 1)
            //{
            //    SilverlightMessageBoxLibrary.Message.ErrorMessage("只能针对一个单位进行职位信息的新增！");
            //    return;
            //}
            ZhaoLuWindow RMWindow = new ZhaoLuWindow(orgids, true, 0, "", islock);
            RMWindow.Closed += new EventHandler(RMWindow_Closed);
            RMWindow.Show();
        }
        void RMWindow_Closed(object sender, EventArgs e)
        {
            Load();
        }
        private string juese = "";
        private void EditData_Click(object sender, RoutedEventArgs e)
        {
            //if (orgids.Count > 1)
            //{
            //    SilverlightMessageBoxLibrary.Message.ErrorMessage("只能针对一个单位进行职位信息的编辑！");
            //    return;
            //}
            selectedempmodel.Clear();
            if (DGRole.ItemsSource != null)
            {
                foreach (ZhaoLuZhiWeiModel i in (ObservableCollection<ZhaoLuZhiWeiModel>)DGRole.ItemsSource)
                {
                    if (i.IsSelected == true && selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() == null)
                    {
                        selectedempmodel.Add(i);
                    }
                    if (selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() != null && i.IsSelected == false)
                    {
                        selectedempmodel.Remove(selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault());
                    }
                }
            }
            if (selectedempmodel.Count > 0)
            {
                foreach (ZhaoLuZhiWeiModel i in selectedempmodel)
                {
                    if (juese == "人事干部" && (i.Status == 1 || i.Status == 3 || i.Status == 4))
                    {
                        SilverlightMessageBoxLibrary.Message.ErrorMessage("所选职位信息状态为不可编辑状态！");
                        return;
                    }
                    if (i.IsLocked == true)
                    {
                        SilverlightMessageBoxLibrary.Message.ErrorMessage("所选职位信息状态为不可编辑状态！");
                        return;
                    }
                }
                if (selectedempmodel.Count > 1)
                {
                    SilverlightMessageBoxLibrary.Message.ErrorMessage("只能选择一条职位信息进行编辑！");
                    return;
                }
                ZhaoLuWindow RMWindow = new ZhaoLuWindow(orgids, false, selectedempmodel[0].ID, "", islock);
                RMWindow.Closed += new EventHandler(RMWindow_Closed);
                RMWindow.Show();

            }
            else SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择要编辑的职位信息！");
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DeleteData_Click(object sender, RoutedEventArgs e)
        {
            selectedempmodel.Clear();
            if (DGRole.ItemsSource != null)
            {
                foreach (ZhaoLuZhiWeiModel i in (ObservableCollection<ZhaoLuZhiWeiModel>)DGRole.ItemsSource)
                {
                    if (i.IsSelected == true && selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() == null)
                    {
                        selectedempmodel.Add(i);
                    }
                    if (selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() != null && i.IsSelected == false)
                    {
                        selectedempmodel.Remove(selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault());
                    }
                }
            }
            if (selectedempmodel.Count > 0)
            {
                foreach (ZhaoLuZhiWeiModel i in selectedempmodel)
                {
                    if (juese != "人事干部" && i.Status == 1)
                    {
                        SilverlightMessageBoxLibrary.Message.ErrorMessage("所选职位信息状态为不可删除状态！");
                        return;
                    }
                    if (juese != "人事干部" && i.Status == 3)
                    {
                        SilverlightMessageBoxLibrary.Message.ErrorMessage("所选职位信息状态为不可删除状态！");
                        return;
                    }
                    if (juese != "人事干部" && i.Status == 4)
                    {
                        SilverlightMessageBoxLibrary.Message.ErrorMessage("所选职位信息状态为不可删除状态！");
                        return;
                    }
                    if (juese == "人事干部" && (i.Status == 1 || i.Status == 3 || i.Status == 4))
                    {
                        SilverlightMessageBoxLibrary.Message.ErrorMessage("所选职位信息状态为不可删除状态！");
                        return;
                    }
                    if (i.IsLocked == true)
                    {
                        SilverlightMessageBoxLibrary.Message.ErrorMessage("所选职位信息状态为不可删除状态！");
                        return;
                    }
                }
                if (MessageBox.Show("确定删除吗？", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    string ids = "";

                    foreach (ZhaoLuZhiWeiModel i in selectedempmodel)
                    {
                        ids += i.ID.ToString() + ",";
                    }
                    Client1.DeteleZhaoLuZhiWeiCompleted -= new EventHandler<DeteleZhaoLuZhiWeiCompletedEventArgs>(Client1_DeteleZhaoLuZhiWeiCompleted);
                    Client1.DeteleZhaoLuZhiWeiCompleted +=new EventHandler<DeteleZhaoLuZhiWeiCompletedEventArgs>(Client1_DeteleZhaoLuZhiWeiCompleted);
                    Client1.DeteleZhaoLuZhiWeiAsync("(" + ids.Substring(0, ids.Length - 1) + ")");
                }

            }
            else SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择要删除的职位信息！");
        }


        private void SaveData_Click(object sender, RoutedEventArgs e)
        {
            selectedempmodel.Clear();
            if (DGRole.ItemsSource != null)
            {
                foreach (ZhaoLuZhiWeiModel i in (ObservableCollection<ZhaoLuZhiWeiModel>)DGRole.ItemsSource)
                {
                    if (i.IsSelected == true && selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() == null)
                    {
                        selectedempmodel.Add(i);
                    }
                    if (selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() != null && i.IsSelected == false)
                    {
                        selectedempmodel.Remove(selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault());
                    }
                }
            }
            if (selectedempmodel.Count > 0)
            {
                foreach (ZhaoLuZhiWeiModel i in selectedempmodel)
                {
                    if (i.Status == 1 || i.Status == 3 || i.Status == 4 || i.IsLocked == true)
                    {
                        SilverlightMessageBoxLibrary.Message.ErrorMessage("所选职位信息状态为不可提交状态！");
                        return;
                    }
                }
                if (MessageBox.Show("确定提交吗？", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    string ids = "";

                    foreach (ZhaoLuZhiWeiModel i in selectedempmodel)
                    {
                        ids += i.ID.ToString() + ",";
                    }
                    
                    Client1.SaveZhaoLuZhiWeiCompleted -= new EventHandler<SaveZhaoLuZhiWeiCompletedEventArgs>(Client1_SaveZhaoLuZhiWeiCompleted);
                    Client1.SaveZhaoLuZhiWeiCompleted+=new EventHandler<SaveZhaoLuZhiWeiCompletedEventArgs>(Client1_SaveZhaoLuZhiWeiCompleted);
                    Client1.SaveZhaoLuZhiWeiAsync("(" + ids.Substring(0, ids.Length - 1) + ")");
                }

            }
            else SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择要提交的职位信息！");
        }

        private void CheckData_Click(object sender, RoutedEventArgs e)
        {
            selectedempmodel.Clear();
            if (DGRole.ItemsSource != null)
            {
                foreach (ZhaoLuZhiWeiModel i in (ObservableCollection<ZhaoLuZhiWeiModel>)DGRole.ItemsSource)
                {
                    if (i.IsSelected == true && selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() == null)
                    {
                        selectedempmodel.Add(i);
                    }
                    if (selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() != null && i.IsSelected == false)
                    {
                        selectedempmodel.Remove(selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault());
                    }
                }
            }
            if (selectedempmodel.Count > 0)
            {
                foreach (ZhaoLuZhiWeiModel i in selectedempmodel)
                {
                    if (i.Status == 0 || i.Status == 2 || i.Status == 3 || i.Status == 4 || i.IsLocked == true)
                    {
                        SilverlightMessageBoxLibrary.Message.ErrorMessage("所选职位信息状态为不可审核状态！");
                        return;
                    }
                }
                if (MessageBox.Show("确定通过审核吗？", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    string ids = "";

                    foreach (ZhaoLuZhiWeiModel i in selectedempmodel)
                    {
                        ids += i.ID.ToString() + ",";
                    }
                    Client1.CheckZhaoLuZhiWeiCompleted -= new EventHandler<CheckZhaoLuZhiWeiCompletedEventArgs>(Client1_CheckZhaoLuZhiWeiCompleted);
                    Client1.CheckZhaoLuZhiWeiCompleted += new EventHandler<CheckZhaoLuZhiWeiCompletedEventArgs>(Client1_CheckZhaoLuZhiWeiCompleted);
                    Client1.CheckZhaoLuZhiWeiAsync("(" + ids.Substring(0, ids.Length - 1) + ")");
                }

            }
            else SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择要通过审核的职位信息！");
        }

        void Client1_CheckZhaoLuZhiWeiCompleted(object sender, CheckZhaoLuZhiWeiCompletedEventArgs e)
        {
            SilverlightMessageBoxLibrary.Message.InfoMessage("审核成功！");
            Load();
        }

        private void TuiData_Click(object sender, RoutedEventArgs e)
        {
            selectedempmodel.Clear();
            if (DGRole.ItemsSource != null)
            {
                foreach (ZhaoLuZhiWeiModel i in (ObservableCollection<ZhaoLuZhiWeiModel>)DGRole.ItemsSource)
                {
                    if (i.IsSelected == true && selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() == null)
                    {
                        selectedempmodel.Add(i);
                    }
                    if (selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() != null && i.IsSelected == false)
                    {
                        selectedempmodel.Remove(selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault());
                    }
                }
            }
            if (selectedempmodel.Count > 0)
            {
                foreach (ZhaoLuZhiWeiModel i in selectedempmodel)
                {
                    if (i.Status == 0 || i.Status == 2 || i.Status == 4 || i.IsLocked == true)
                    {
                        SilverlightMessageBoxLibrary.Message.ErrorMessage("所选职位信息状态为不可退回状态！");
                        return;
                    }
                }
                TuiHuiMemoWindow tuihui = new TuiHuiMemoWindow();
                tuihui.Closed += new EventHandler(tuihui_Closed);
                tuihui.Show();
            }
            else SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择要退回的职位信息！");
        }

        void tuihui_Closed(object sender, EventArgs e)
        {
            if (MessageBox.Show("确定退回吗？", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                string ids = "";

                foreach (ZhaoLuZhiWeiModel i in selectedempmodel)
                {
                    ids += i.ID.ToString() + ",";
                }
                Client1.TuiZhaoLuZhiWeiCompleted -= new EventHandler<TuiZhaoLuZhiWeiCompletedEventArgs>(Client1_TuiZhaoLuZhiWeiCompleted);
                Client1.TuiZhaoLuZhiWeiCompleted += new EventHandler<TuiZhaoLuZhiWeiCompletedEventArgs>(Client1_TuiZhaoLuZhiWeiCompleted);
                Client1.TuiZhaoLuZhiWeiAsync("(" + ids.Substring(0, ids.Length - 1) + ")",
                                             ((TuiHuiMemoWindow) sender).checkmemo);
            }
        }

        void Client1_TuiZhaoLuZhiWeiCompleted(object sender, TuiZhaoLuZhiWeiCompletedEventArgs e)
        {
            SilverlightMessageBoxLibrary.Message.InfoMessage("退回成功！");
            Load();
        }


        private void ChaData_Click(object sender, RoutedEventArgs e)
        {
            string ss = "";
            foreach (string ii in orgids)
                ss += ii + ",";
            if (string.IsNullOrEmpty(ss)) ss = "0";
            else ss = ss.Substring(0, ss.Length - 1);
            Client1.ChaKanShiFouDouShenHeZhaoLuZhiWeiCompleted -= new EventHandler<ChaKanShiFouDouShenHeZhaoLuZhiWeiCompletedEventArgs>(Client1_ChaKanShiFouDouShenHeZhaoLuZhiWeiCompleted);
            Client1.ChaKanShiFouDouShenHeZhaoLuZhiWeiCompleted += new EventHandler<ChaKanShiFouDouShenHeZhaoLuZhiWeiCompletedEventArgs>(Client1_ChaKanShiFouDouShenHeZhaoLuZhiWeiCompleted);
            Client1.ChaKanShiFouDouShenHeZhaoLuZhiWeiAsync(ss, DateTime.Today.Year - nian.SelectedIndex, islock);
            Client1.GetZhaoLuZhiWeiCountCompleted -= new EventHandler<GetZhaoLuZhiWeiCountCompletedEventArgs>(Client1_GetZhaoLuZhiWeiCountCompleted);
            Client1.GetZhaoLuZhiWeiCountCompleted += new EventHandler<GetZhaoLuZhiWeiCountCompletedEventArgs>(Client1_GetZhaoLuZhiWeiCountCompleted);
            Client1.GetZhaoLuZhiWeiCountAsync(ss, 0, Convert.ToInt32(((ComboBoxItem)status.SelectedItem).Tag), DateTime.Today.Year - nian.SelectedIndex, islock);
            Client1.GetZhaoLuZhiWeiCompleted -= new EventHandler<GetZhaoLuZhiWeiCompletedEventArgs>(Client1_GetZhaoLuZhiWeiCompleted);
            Client1.GetZhaoLuZhiWeiCompleted += new EventHandler<GetZhaoLuZhiWeiCompletedEventArgs>(Client1_GetZhaoLuZhiWeiCompleted);
            Client1.GetZhaoLuZhiWeiAsync(ss, 0, 0, 20, Convert.ToInt32(((ComboBoxItem)status.SelectedItem).Tag), DateTime.Today.Year - nian.SelectedIndex, islock);

        }

        private void FaBuData_Click(object sender, RoutedEventArgs e)
        {
            selectedempmodel.Clear();
            if (DGRole.ItemsSource != null)
            {
                foreach (ZhaoLuZhiWeiModel i in (ObservableCollection<ZhaoLuZhiWeiModel>)DGRole.ItemsSource)
                {
                    if (i.IsSelected == true && selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() == null)
                    {
                        selectedempmodel.Add(i);
                    }
                    if (selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() != null && i.IsSelected == false)
                    {
                        selectedempmodel.Remove(selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault());
                    }
                }
            }
            if (selectedempmodel.Count > 0)
            {
                foreach (ZhaoLuZhiWeiModel i in selectedempmodel)
                {
                    if (i.Status != 3 || i.IsLocked == true)
                    {
                        SilverlightMessageBoxLibrary.Message.ErrorMessage("所选职位信息状态为不可发布状态！");
                        return;
                    }
                }
                if (MessageBox.Show("确定发布吗？", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    string ids = "";

                    foreach (ZhaoLuZhiWeiModel i in selectedempmodel)
                    {
                        ids += i.ID.ToString() + ",";
                    }
                    Client1.FaBuZhaoLuZhiWeiCompleted -= new EventHandler<FaBuZhaoLuZhiWeiCompletedEventArgs>(Client1_FaBuZhaoLuZhiWeiCompleted);
                    Client1.FaBuZhaoLuZhiWeiCompleted += new EventHandler<FaBuZhaoLuZhiWeiCompletedEventArgs>(Client1_FaBuZhaoLuZhiWeiCompleted);
                    Client1.FaBuZhaoLuZhiWeiAsync("(" + ids.Substring(0, ids.Length - 1) + ")");
                }

            }
            else SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择要发布的职位信息！");
        }

        void Client1_FaBuZhaoLuZhiWeiCompleted(object sender, FaBuZhaoLuZhiWeiCompletedEventArgs e)
        {
            SilverlightMessageBoxLibrary.Message.InfoMessage("发布成功！");
            Load();
        }

        private void btnSuoData_Click(object sender, RoutedEventArgs e)
        {
            //selectedempmodel.Clear();
            //if (DGRole.ItemsSource != null)
            //{
            //    foreach (PublicOfficialSelectionService.PublicOfficialSelectionModel i in (ObservableCollection<PublicOfficialSelectionService.PublicOfficialSelectionModel>)DGRole.ItemsSource)
            //    {
            //        if (i.IsSelected == true && selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() == null)
            //        {
            //            selectedempmodel.Add(i);
            //        }
            //        if (selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() != null && i.IsSelected == false)
            //        {
            //            selectedempmodel.Remove(selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault());
            //        }
            //    }
            //}
            //if (selectedempmodel.Count > 0)
            //{
            if (MessageBox.Show("确定锁定本期工作吗？", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                Client1.SuoZhaoLuZhiWeiCompleted -= new EventHandler<SuoZhaoLuZhiWeiCompletedEventArgs>(Client1_SuoZhaoLuZhiWeiCompleted);
                Client1.SuoZhaoLuZhiWeiCompleted += new EventHandler<SuoZhaoLuZhiWeiCompletedEventArgs>(Client1_SuoZhaoLuZhiWeiCompleted);
                Client1.SuoZhaoLuZhiWeiAsync();
            }

            //}
            //else SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择要发布的职位信息！");
        }

        void Client1_SuoZhaoLuZhiWeiCompleted(object sender, SuoZhaoLuZhiWeiCompletedEventArgs e)
        {
            SilverlightMessageBoxLibrary.Message.InfoMessage("锁定成功！");
            Load();
        }

        private void CancelCheckData_Click(object sender, RoutedEventArgs e)
        {
            selectedempmodel.Clear();
            if (DGRole.ItemsSource != null)
            {
                foreach (ZhaoLuZhiWeiModel i in (ObservableCollection<ZhaoLuZhiWeiModel>)DGRole.ItemsSource)
                {
                    if (i.IsSelected == true && selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() == null)
                    {
                        selectedempmodel.Add(i);
                    }
                    if (selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() != null && i.IsSelected == false)
                    {
                        selectedempmodel.Remove(selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault());
                    }
                }
            }
            if (selectedempmodel.Count > 0)
            {
                foreach (ZhaoLuZhiWeiModel i in selectedempmodel)
                {
                    if (i.Status != 3 || i.IsLocked == true)
                    {
                        SilverlightMessageBoxLibrary.Message.ErrorMessage("所选职位信息状态不为已审核，不可取消审核！");
                        return;
                    }
                }
                if (MessageBox.Show("确定取消审核吗？", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    string ids = "";

                    foreach (ZhaoLuZhiWeiModel i in selectedempmodel)
                    {
                        ids += i.ID.ToString() + ",";
                    }
                    Client1.CancelZhaoLuZhiWeiCompleted -= new EventHandler<CancelZhaoLuZhiWeiCompletedEventArgs>(Client1_CancelZhaoLuZhiWeiCompleted);
                    Client1.CancelZhaoLuZhiWeiCompleted += new EventHandler<CancelZhaoLuZhiWeiCompletedEventArgs>(Client1_CancelZhaoLuZhiWeiCompleted);
                    Client1.CancelZhaoLuZhiWeiAsync("(" + ids.Substring(0, ids.Length - 1) + ")");
                }

            }
            else SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择要发布的职位信息！");
        }

        void Client1_CancelZhaoLuZhiWeiCompleted(object sender, CancelZhaoLuZhiWeiCompletedEventArgs e)
        {
            SilverlightMessageBoxLibrary.Message.InfoMessage("取消审核成功！");
            Load();
        }

        private void ViewData_Click(object sender, RoutedEventArgs e)
        {
            if (DGRole.ItemsSource != null)
            {
                foreach (ZhaoLuZhiWeiModel i in (ObservableCollection<ZhaoLuZhiWeiModel>)DGRole.ItemsSource)
                {
                    if (i.IsSelected == true && selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() == null)
                    {
                        selectedempmodel.Add(i);
                    }
                    if (selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() != null && i.IsSelected == false)
                    {
                        selectedempmodel.Remove(selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault());
                    }
                }
            }
            if (selectedempmodel.Count > 0)
            {
                if (selectedempmodel.Count > 1)
                {
                    SilverlightMessageBoxLibrary.Message.ErrorMessage("只能选择一条职位信息进行查看！");
                    return;
                }
                ZhaoLuWindow RMWindow = new ZhaoLuWindow(orgids, false, selectedempmodel[0].ID, "查看", islock);
                RMWindow.Closed += new EventHandler(RMWindow_Closed);
                RMWindow.Show();

            }
            else SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择要查看的职位信息！");
        }

        #endregion
    }
}
